import React from 'react';
import { Button, Space, Table, Tag } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import { Link } from 'react-router-dom'
import './Account.css';
import request from '../../api/server'

enum Type {
  income = "收入",
  expenditure = "支出",
}

interface DataType {
  key: string;
  title: string;
  type: Type;
  remark: string;
  purpose: string;
}
const update = (data: DataType) => {
  console.log(data)
};
const getData = async () => {
  console.log()
  let res = await request({
    url: '/api/accounts/json',
    method: 'get',
  })
  console.log(typeof(res))
}
const columns: ColumnsType<DataType> = [
  {
    title: '标题',
    dataIndex: 'title',
    key: 'title',
  },
  {
    title: '类型',
    dataIndex: 'type',
    key: 'type',
    render: (text) => {
      if (text === Type.expenditure) {
        return <Tag bordered={false} color="#f50">
          {text}
        </Tag>
      }
      if (text === Type.income) {
        return <Tag bordered={false} color="#2db7f5">
          {text}
        </Tag>
      }
    },
  },
  {
    title: '用途',
    dataIndex: 'purpose',
    key: 'purpose',
  },
  {
    title: '备注',
    dataIndex: 'remark',
    key: 'remark',
  },
  {
    title: '操作',
    key: 'action',
    render: (_, record) => (
      <Space size="middle">
        <Button onClick={() => update(record)}>编辑</Button>
      </Space>
    ),
  },
];

const data: DataType[] = [
  {
    key: '1',
    title: 'John Brown',
    type: Type.income,
    purpose: '工资',
    remark: 'New York No. 1 Lake Park',
  },
  {
    key: '2',
    title: 'Jim Green',
    type: Type.expenditure,
    purpose: '买衣服',
    remark: 'London No. 1 Lake Park',
  },
  {
    key: '3',
    title: 'Joe Black',
    type: Type.income,
    purpose: '兼职',
    remark: 'Sydney No. 1 Lake Park',
  },
];

const Account: React.FC = () => (
  <div>
    <div className='flex-between'>
      <h1 onClick={() => { getData() }}>账单列表</h1>
      <Link to="/">
        返回首页
      </Link>
    </div>
    <Table columns={columns} dataSource={data} />
  </div>

)
export default Account;